package com.ych.spring.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author yangchenghao
 */
@Component
public class LoggerAspect {
    public void pointcut() {
    }

    public void beforeMethod(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("日志：方法" + signature.getName() + ",参数" + Arrays.toString(joinPoint.getArgs()));
    }

    public void AfterMethod(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("日志：方法" + signature.getName() + "执行完毕");
    }

    public void AfterReturnMethod(JoinPoint joinPoint, Object result) {
        Signature signature = joinPoint.getSignature();
        System.out.println("日志：方法" + signature.getName() + "，结果：" + result);
    }

    public void AfterThrowMethod(JoinPoint joinPoint, Exception e) {
        Signature signature = joinPoint.getSignature();
        System.out.println("日志：方法" + signature.getName() + "，异常" + e);
    }

    public Object AroundMethod(ProceedingJoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        Object result = null;
        try {
            System.out.println("日志：方法" + signature.getName() + "，参数：" + Arrays.toString(joinPoint.getArgs()));
            result = joinPoint.proceed();
            System.out.println("日志：方法" + signature.getName() + "，结果" + result);
        } catch (Throwable e) {
            System.out.println("日志：方法" + signature.getName() + "，异常：" + e);
        } finally {
            System.out.println("日志：方法" + signature.getName() + "执行完毕");
        }
        return result;

    }
}
